package com.example.demo.controller;

import com.example.demo.service.CommonService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @auther sc
 * @Date 2017/1
 */
@RestController
@RefreshScope
public class InfoController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EurekaClient eurekaClient;
    @Autowired
    private CommonService commonService;
    @Autowired
    private FeignController feignController;

    @Value(("${name}"))
    private String name;
    @Value(("${company}"))
    private String company;

    @GetMapping("/eureka-instance")
    public String serviceUrl(){
        InstanceInfo instanceInfo = this.eurekaClient.getNextServerFromEureka("CLIENT1",false);
        return  instanceInfo.getHomePageUrl();
    }


    @RequestMapping("/info")
    public String info(){
        return "I am service-sc";
    }

    @GetMapping("/test")
    public String foo1(){
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://provide/user/{1}/{2}",String.class,"ddd",12);
        String body = responseEntity.getBody();
      //  responseEntity.getStatusCode();
        return body;
    }

    //RestTemplate使用
    @GetMapping("/getuser")
    public User findUser(){
        int id=1;
        return this.restTemplate.getForObject("http://provide/user/"+id,User.class);
    }




    @GetMapping("/hi")
    public String hi(){

        String reslut =  this.restTemplate.getForObject("http://provide/hi?name=sc",String.class);
        System.out.println(reslut);
        return reslut;
    }


    @GetMapping("/hh")
    public String hh(){
        return commonService.foo1();
    }


    @RequestMapping("/sc")
    public String sc(String name){
        return feignController.hello(name);
    }

    @GetMapping("/conf")
    public String conf(){
        return name+company+"/gitee";
    }
}
